﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Happy.Query.Filters;

namespace Happy.Application.Query
{
    /// <summary>
    /// 客户端传递的动态筛选器。
    /// </summary>
    public sealed class DynamicFilter
    {
        /// <summary>
        /// 连接器。
        /// </summary>
        public Connector Connector { get; set; }

        /// <summary>
        /// 属性。
        /// </summary>
        public string Property { get; set; }

        /// <summary>
        /// 运算符。
        /// </summary>
        public Operator Operator { get; set; }

        /// <summary>
        /// 运算数。
        /// </summary>
        public DataValue[] Values { get; set; }

        internal IFilter ToFilter()
        {
            var operands = this.GetOperands();

            return new Filter(this.Connector, this.Property, this.Operator, operands);
        }

        private Operand[] GetOperands()
        {
            return this.Values.Select(x =>
            {
                return new Operand(OperandType.Constant, x.Value);
            }).ToArray();
        }
    }
}
